package com.zego.wawaji.ui.center.pay;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.zego.wawaji.R;
import com.zego.wawaji.base.log.PToast;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.base.observer.PObserver;
import com.zego.wawaji.base.utils.UIUtil;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.third.pay.BasePayPannel;
import com.zego.wawaji.third.pay.PayAlipayPannel;
import com.zego.wawaji.third.pay.PayWXPannel;
import com.zego.wawaji.third.pay.util.PayClickListener;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.base.recycler.BaseRecyclerViewHolder;
import com.zego.wawaji.ui.bean.pay.Pay;
import com.zego.wawaji.ui.bean.pay.PayList;

/**
 * 我的娃娃币：充值页面
 * Created by Su on 2017/11/27.
 */
public class MyPropertyAct extends BaseAppCompatActivity implements PObserver, RequestComplete, View.OnClickListener, BaseRecyclerViewHolder.OnItemClickListener {
    private float toDpMutliple = 1; //根据屏幕密度获取屏幕转换倍数
    private RecyclerView recyclerView;  // 商品列表
    private WawaCoinAdapter adapter;
    private LinearLayout payAlipay, payWX;  // 支付方式
    private BasePayPannel payAlipayPannel, payWXPannel;

    private int position = 0;
    private PayList payList;           // 商品列表
    private boolean isAliPay = true;   // 默认支付宝支付

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_property_act);
        initView();
        reqData();
    }

    private void reqData() {
        ModuleMgr.getWawaMgr().reqPayListNew(this); // 获取商品列表
    }

    private void initView() {
        setBackView();
        setTitle("我的金币");
        MsgMgr.getInstance().attach(this);
        toDpMutliple = UIUtil.toDpMultiple(this);
        initPayType();
        recyclerView = (RecyclerView) findViewById(R.id.pay_list);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerView.addItemDecoration(new ItemSpaces((int) (10 * toDpMutliple)));
        adapter = new WawaCoinAdapter();
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        findViewById(R.id.btn_pay).setOnClickListener(payClickListener);
        refreshCoinNum();
    }

    private void initPayType() {
        payAlipay = (LinearLayout) findViewById(R.id.pay_alipay);
        payAlipayPannel = new PayAlipayPannel(this);
        payAlipay.addView(payAlipayPannel.getContentView());
        payAlipayPannel.setChooseState(true);
        payAlipay.setOnClickListener(this);

        payWX = (LinearLayout) findViewById(R.id.pay_wechat);
        payWXPannel = new PayWXPannel(this);
        payWX.addView(payWXPannel.getContentView());
        payWX.setOnClickListener(this);
    }

    private void refreshCoinNum() {
//        UserDetail userDetail = ModuleMgr.getCenterMgr().getMyInfo();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pay_alipay:
                isAliPay = true;
                payAlipayPannel.setChooseState(true);
                payWXPannel.setChooseState(false);
                break;

            case R.id.pay_wechat:
                isAliPay = false;
                payAlipayPannel.setChooseState(false);
                payWXPannel.setChooseState(true);

                break;
        }
    }

    private PayClickListener payClickListener = new PayClickListener() {
        @Override
        public void onPayClick(View v) {
            switch (v.getId()) {
                case R.id.btn_pay:
                    pay();
                    break;
            }
        }
    };

    private void pay() {
        if (payList == null) {
            PToast.showShort("商品请求出错，请重试");
            return;
        }

        if (getPosition() < 0) {
            PToast.showShort("请选择支付商品");
            return;
        }

        Pay data = payList.getPayList().get(getPosition());
        if (isAliPay) {    // 支付宝支付
            ModuleMgr.getWawaMgr().reqAlipay("金币" + (data.getWwCoinCz() + data.getWwCoinZs()) + "个", data.getId(), null);
            return;
        }
        ModuleMgr.getWawaMgr().reqWXPay("金币" + (data.getWwCoinCz() + data.getWwCoinZs()) + "个", data.getId(), null);
    }

    public int getPosition() {
        return position;
    }

    @Override
    public void onItemClick(View convertView, int position) {
        this.position = position;
        adapter.updateData(position);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqPayListNew) {
            if (response.isOk()) {
                payList = (PayList) response.getBaseData();
                adapter.setList(payList.getPayList());
            }
            return;
        }
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Update_MyInfo:  // 充值成功后，会请求个人资料
                ModuleMgr.getWawaMgr().reqPayListNew(this); // 获取商品列表
                break;

            case MsgType.MT_MyInfo_Change:
                refreshCoinNum();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    private class ItemSpaces extends RecyclerView.ItemDecoration {
        private int space;

        public ItemSpaces(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.right = space / 2;
            outRect.left = space / 2;
            outRect.top = space;
        }
    }
}
